// wAudio.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include <Windows.h>
#include <Endpointvolume.h>
#include <Mmdeviceapi.h>
#include <conio.h>
#include <mmdeviceapi.h>
#include <devicetopology.h>
#include <Ksmedia.h>
#include <math.h>

#pragma comment(lib, "Winmm.lib")

HRESULT _hr;
IMMDeviceEnumerator *_pDeviceEnumerator;
IMMDevice *_pDefaultDevice;

IDeviceTopology *_pDeviceTopology;
IConnector *_pConnectorFrom;
IConnector *_pConnectorTo;
IPart *_pPart;
IAudioVolumeLevel *_pAudioVolumeLevelFront;
IAudioVolumeLevel *_pAudioVolumeLevelRear;
IAudioVolumeLevel *_pAudioVolumeLevelSubwoofer;
IAudioVolumeLevel *_pAudioVolumeLevelCenter;
IAudioVolumeLevel *_pAudioVolumeLevelSide;


#define version		"1.0.0"
#define progname	"wAudio.exe"
int dID = 1;

void usage(void)
{
	fprintf(stderr, "Audio volume control for gaming   Version: %s\n"
					"\n(c) Tody 2018, T-ware Inc.\n"
					"\nUsage:\n\t%s <ID volume>"
					"\nID Options:\n"
					"      1       front volume\n"					
					"      2       Rear volume\n"
					"      3       Subwoofer volume\n"
					"      4       Center volume\n"
					"      5       Side volume\n", version, progname );

	exit(1);
}
float dbToPercent(float value)
{
    return powf(10.0f, (value/20.0f)) * 100.0f;
}

float percentToDb(float value)
{
    return 20 * log10(value/100);
}

void on_hSlider_Front_valueChanged(int value)
{
    if(_pAudioVolumeLevelFront)
    {
        float valueDB =percentToDb(value);
        float valDBs[2] = { valueDB, valueDB };
        _pAudioVolumeLevelFront->SetLevelAllChannels(valDBs, 2, NULL);
    }
}

void on_hSlider_Rear_valueChanged(int value)
{
    if(_pAudioVolumeLevelRear)
    {
        float valueDB = percentToDb(value);
        float valDBs[2] = { valueDB, valueDB };
        _pAudioVolumeLevelRear->SetLevelAllChannels(valDBs, 2, NULL);
    }
}

void on_hSlider_Sub_valueChanged(int value)
{
    if(_pAudioVolumeLevelSubwoofer)
    {
        float valueDB = percentToDb(value);
        _pAudioVolumeLevelSubwoofer->SetLevel(0, valueDB, NULL);
    }
}

void on_hSlider_Center_valueChanged(int value)
{
    if(_pAudioVolumeLevelCenter)
    {
        float valueDB = percentToDb(value);
        _pAudioVolumeLevelCenter->SetLevel(0, valueDB, NULL);
    }
}

void on_hSlider_Side_valueChanged(int value)
{
    if(_pAudioVolumeLevelSide)
    {
        float valueDB = percentToDb(value);
        float valDBs[2] = { valueDB, valueDB };
        _pAudioVolumeLevelSide->SetLevelAllChannels(valDBs, 2, NULL);
    }
}


int main(int argc, char* argv[])
{
	float ch0, ch1;
	int volume=100;

	if (argc!=3)
		usage();

	if (atoi(argv[1])<1 || atoi(argv[1])>5) 
		usage();

	if (atoi(argv[2])>100 || atoi(argv[2])<0)
		usage();

	dID = atoi(argv[1]);
	volume=atoi(argv[2]);

    // Initialize COM component
	_hr = CoInitialize(NULL);

	if(FAILED(_hr)){
		printf("CoInitialize failed");
		return 1;
	}

    _hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_INPROC_SERVER, __uuidof(IMMDeviceEnumerator), (LPVOID *)&_pDeviceEnumerator);
    if (_hr != S_OK){
        fprintf(stderr,"MMDeviceEnumerator failed\n");
		return 1;
	}

    _hr = _pDeviceEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, &_pDefaultDevice); // need default device in mmsys.cpl Speakers
    if (_hr != S_OK){
		fprintf(stderr,"GetDefaultAudioEndpoint failed\n");
		return 1;
	}

    _hr = _pDefaultDevice->Activate(__uuidof(IDeviceTopology), CLSCTX_INPROC_SERVER, NULL, (PVOID *)&_pDeviceTopology);
    if (_hr != S_OK){
		fprintf(stderr,"IDeviceTopology failed\n");
		return 1;
	}

    _hr = _pDeviceTopology->GetConnector(0, &_pConnectorFrom);
    if (_hr != S_OK){
        fprintf(stderr,"GetConnector failed\n");
		return 1;
	}

    _hr = _pConnectorFrom->GetConnectedTo(&_pConnectorTo);
    if (_hr != S_OK){
        fprintf(stderr,"GetConnectedTo failed\n");
		return 1;
	}

    _hr = _pConnectorTo->QueryInterface(__uuidof(IPart), (PVOID *)&_pPart);
    if (_hr != S_OK){
        fprintf(stderr,"QueryInterface failed\n");
		return 1;
	}

    LPWSTR PartName = nullptr;
    _pPart->GetName(&PartName);
	wprintf(L"> %s\n", PartName);  // Realtek High Definition Audio
    CoTaskMemFree(PartName);
    PartName = nullptr;

    IPartsList *pPartList = nullptr;
    _hr = _pPart->EnumPartsIncoming(&pPartList);
    if(_hr != S_OK){
        fprintf(stderr,"EnumPartsIncoming failed\n");
		return 1;
	}

    IPart *pPartItem1 = nullptr;
    _hr = pPartList->GetPart(0, &pPartItem1);
    if(_hr != S_OK){
        fprintf(stderr,"GetPart failed\n");
		return 1;
	}

    LPWSTR PartNameItem1 = nullptr;
    pPartItem1->GetName(&PartNameItem1);
	wprintf(L"> %s\n", PartNameItem1);
    CoTaskMemFree(PartNameItem1);
    PartNameItem1 = nullptr;

    // Find GUID/KSNODE in Ksmedia.h and see what interface you can init
    ///// https://msdn.microsoft.com/en-us/library/windows/desktop/dd371442(v=vs.85).aspx
    GUID guidPartItem1;
    _hr = pPartItem1->GetSubType(&guidPartItem1); // KSNODETYPE_MUTE pPartItem1->Activate(IAudioMute) (can control Master Mute of Realtek HD Audio output)
    if(_hr != S_OK){
        fprintf(stderr,"GetSubType PartItem1 failed\n");
		return 1;
	}

    IPartsList *pPartList2 = nullptr;
    IPart *pPartItem2 = nullptr;

    _hr = pPartItem1->EnumPartsIncoming(&pPartList2);
    if(_hr != S_OK){
        fprintf(stderr,"EnumPartsIncoming from PartItem1 failed\n");
		return 1;
	}

    _hr = pPartList2->GetPart(0, &pPartItem2);
    if(_hr != S_OK){
        fprintf(stderr,"GetPart PartList2 failed\n");
		return 1;
	}

    LPWSTR PartNameItem2 = nullptr;
    pPartItem2->GetName(&PartNameItem2);
	wprintf(L"> %s\n", PartNameItem2);
    CoTaskMemFree(PartNameItem2);
    PartNameItem2 = nullptr;

    GUID guidPartItem2;
    _hr = pPartItem2->GetSubType(&guidPartItem2); // KSNODETYPE_VOLUME pPartItem2->Activate(IAudioVolumeLevel) (can control Master Volume of Realtek HD Audio output)
    if(_hr != S_OK){
        fprintf(stderr,"GetSubType PartItem2 failed\n");
		return 1;
	}

    IPartsList *pPartList3 = nullptr;
    _hr = pPartItem2->EnumPartsIncoming(&pPartList3);
    if(_hr != S_OK){
        fprintf(stderr,"EnumPartsIncoming PartItem2 failed\n");
		return 1;
	}

    IPart *pPartItem3 = nullptr;
    pPartList3->GetPart(0, &pPartItem3);
    if(_hr != S_OK){
        fprintf(stderr,"GetPart PartItem3 failed\n");
		return 1;
	}

    LPWSTR PartNameItem3;
    pPartItem3->GetName(&PartNameItem3);
	wprintf(L"> %s\n", PartNameItem3);
    CoTaskMemFree(PartNameItem3);
    PartNameItem3 = nullptr;

    GUID guidPartItem3;
    _hr = pPartItem3->GetSubType(&guidPartItem3); // KSNODETYPE_SUM (Front, Rear, Subwoofer, Center, Side, ...)
    if(_hr != S_OK){
        fprintf(stderr,"GetSubType PartItem3 failed\n");
		return 1;
	}

    IPartsList *pPartList4 = nullptr;
    _hr = pPartItem3->EnumPartsIncoming(&pPartList4);
    if(_hr != S_OK){
        fprintf(stderr,"EnumPartsIncoming PartItem3 failed\n");
		//return 1;
	}

    UINT CountLevels;
    pPartList4->GetCount(&CountLevels); // PartList4Count

	printf("> Count Levels: %d\n", CountLevels);

    // (Front, Rear, Subwoofer, Center, Side, ...)
    // -------
    // Front
    IPart *pPartItemFront = nullptr;
    pPartList4->GetPart(CountLevels-1, &pPartItemFront);

    LPWSTR PartItem4Name;
    pPartItemFront->GetName(&PartItem4Name);
	wprintf(L"> %s\n", PartItem4Name);
    CoTaskMemFree(PartItem4Name);
    PartItem4Name = nullptr;

    GUID guidPartItem4;
    _hr = pPartItemFront->GetSubType(&guidPartItem4); // KSNODETYPE_VOLUME pPartItemLast->Activate(IAudioVolumeLevel)
    if(_hr != S_OK){
        fprintf(stderr,"GetSubType Front failed\n");
		//return 1;
	}
    
	_hr = pPartItemFront->Activate(CLSCTX_INPROC, __uuidof(IAudioVolumeLevel), (PVOID *)&_pAudioVolumeLevelFront);
    if(_hr != S_OK){
        fprintf(stderr,"Activate Front failed\n");
		//return 1;
	}

    if(pPartItemFront)
    {
        pPartItemFront->Release();
        pPartItemFront = nullptr;
    }


    // -------
    // Side
    IPart *pPartItemRear = nullptr;
    pPartList4->GetPart(CountLevels-2, &pPartItemRear);

    LPWSTR PartItem5Name;
    pPartItemRear->GetName(&PartItem5Name);
	wprintf(L"> %s\n", PartItem5Name);
    CoTaskMemFree(PartItem5Name);
    PartItem5Name = nullptr;

    _hr = pPartItemRear->Activate(CLSCTX_INPROC, __uuidof(IAudioVolumeLevel), (PVOID *)&_pAudioVolumeLevelRear);
    if(_hr != S_OK){
        fprintf(stderr,"Activate Side failed\n");
		//return 1;
	}

    if(pPartItemRear)
    {
        pPartItemRear->Release();
        pPartItemRear = nullptr;
    }


    // -------
    // Subwoofer
    IPart *pPartItemSubwoofer = nullptr;
    pPartList4->GetPart(CountLevels-3, &pPartItemSubwoofer);

    LPWSTR PartItem6Name;
    pPartItemSubwoofer->GetName(&PartItem6Name);
	wprintf(L"> %s\n", PartItem6Name);
    CoTaskMemFree(PartItem6Name);
    PartItem6Name = nullptr;

    _hr = pPartItemSubwoofer->Activate(CLSCTX_INPROC, __uuidof(IAudioVolumeLevel), (PVOID *)&_pAudioVolumeLevelSubwoofer);
    if(_hr != S_OK){
        fprintf(stderr,"Activate Subwoofer failed\n");
		//return 1;
	}

    if(pPartItemSubwoofer)
    {
        pPartItemSubwoofer->Release();
        pPartItemSubwoofer = nullptr;
    }


    // -------
    // Center
    IPart *pPartItemCenter = nullptr;
    pPartList4->GetPart(CountLevels-4, &pPartItemCenter);

    LPWSTR PartItem7Name;
    pPartItemCenter->GetName(&PartItem7Name);
	wprintf(L"> %s\n", PartItem7Name);
    CoTaskMemFree(PartItem7Name);
    PartItem7Name = nullptr;

    _hr = pPartItemCenter->Activate(CLSCTX_INPROC, __uuidof(IAudioVolumeLevel), (PVOID *)&_pAudioVolumeLevelCenter);
    if(_hr != S_OK){
        fprintf(stderr,"Activate Center failed\n");
		//return 1;
	}

    if(pPartItemCenter)
    {
        pPartItemCenter->Release();
        pPartItemCenter = nullptr;
    }

/*
    // -------
    // Mute
    IPart *pPartItemSide = nullptr;
    pPartList4->GetPart(CountLevels-5, &pPartItemSide);

    LPWSTR PartItem8Name;
    pPartItemSide->GetName(&PartItem8Name);
	wprintf(L"8: %s\n", PartItem8Name);
    CoTaskMemFree(PartItem8Name);
    PartItem8Name = nullptr;

    _hr = pPartItemSide->Activate(CLSCTX_INPROC, __uuidof(IAudioVolumeLevel), (PVOID *)&_pAudioVolumeLevelSide);
    if(_hr != S_OK){
        fprintf(stderr,"Activate failed\n");
		return 1;
	}

	if(pPartItemSide)
    {
        pPartItemSide->Release();
        pPartItemSide = nullptr;
    }
*/
	printf("> Control code: <%d %d>\n\n\tSetting volume....\n", dID, volume);
	/// control code

	switch(dID){
		case 1:   // Front
			on_hSlider_Front_valueChanged(volume);
			_pAudioVolumeLevelFront->GetLevel(0, &ch0);
			_pAudioVolumeLevelFront->GetLevel(1, &ch1);
			printf("\n\t[Front]# Left: %3.0f,  Right: %3.0f", dbToPercent(ch0), dbToPercent(ch1));
			break;

		case 2:   // Rear
			on_hSlider_Rear_valueChanged(volume);
			_pAudioVolumeLevelRear->GetLevel(0, &ch0);
			_pAudioVolumeLevelRear->GetLevel(1, &ch1);
			printf("\n\t[Rear]# Left: %3.0f,  Right: %3.0f", dbToPercent(ch0), dbToPercent(ch1));
			break;

		case 3:	 // Subwoofer	
			on_hSlider_Sub_valueChanged(volume);
			_pAudioVolumeLevelSubwoofer->GetLevel(0, &ch0);
			printf("\n\t[Subwoofer]# %3.0f", dbToPercent(ch0));
			break;
	
		case 4:  // Center
			on_hSlider_Center_valueChanged(volume);
			_pAudioVolumeLevelCenter->GetLevel(0, &ch0);
			printf("\n\t[Center]# %3.0f", dbToPercent(ch0));
			break;

		case 5:  // Side
			on_hSlider_Side_valueChanged(volume);
			_pAudioVolumeLevelSide->GetLevel(0, &ch0);
			_pAudioVolumeLevelSide->GetLevel(1, &ch1);
			printf("\n\t[Side]# Left: %3.0f,  Right: %3.0f", dbToPercent(ch0), dbToPercent(ch1));
			break;
	}

    if(_pAudioVolumeLevelFront)
    {
        _pAudioVolumeLevelFront->Release();
        _pAudioVolumeLevelFront = nullptr;
    }

    if(_pAudioVolumeLevelRear)
    {
        _pAudioVolumeLevelRear->Release();
        _pAudioVolumeLevelRear = nullptr;
    }

    if(_pAudioVolumeLevelSubwoofer)
    {
        _pAudioVolumeLevelSubwoofer->Release();
        _pAudioVolumeLevelSubwoofer = nullptr;
    }

    if(_pAudioVolumeLevelCenter)
    {
        _pAudioVolumeLevelCenter->Release();
        _pAudioVolumeLevelCenter = nullptr;
    }

    if(_pAudioVolumeLevelSide)
    {
        _pAudioVolumeLevelSide->Release();
        _pAudioVolumeLevelSide = nullptr;
    }

    if(_pPart)
    {
        _pPart->Release();
        _pPart = nullptr;
    }

    if(_pConnectorTo)
    {
        _pConnectorTo->Release();
        _pConnectorTo = nullptr;
    }

    if(_pConnectorFrom)
    {
        _pConnectorFrom->Release();
        _pConnectorFrom = nullptr;
    }

    if(_pDeviceTopology)
    {
        _pDeviceTopology->Release();
        _pDeviceTopology = nullptr;
    }

    if(_pDefaultDevice)
    {
        _pDefaultDevice->Release();
        _pDefaultDevice = nullptr;
    }

    if(_pDeviceEnumerator)
    {
        _pDeviceEnumerator->Release();
        _pDeviceEnumerator = nullptr;
    }

	CoUninitialize();
	return 0;
}

